home *** CD-ROM | disk | FTP | other *** search
- // Dynamic link library implementation of a decimator with running average filtering
-
- #include "NSDLL.h"
-
- #define buffer(i, j) bufferData->data[j+(i)*bufferData->length]
-
- typedef struct {
- int count;
- int length;
- NSFloat *data;
- } DecimatorData;
-
- /***************************/
- /* Activation of component */
- __declspec(dllexport) BOOL performPrePost(
- DLLData *instance, // Pointer to instance data (may be NULL)
- NSFloat *input, // Pointer to the input data
- NSFloat *output, // Pointer to the output data
- int rows, // Number of rows of data
- int cols, // Number of cols of data
- BOOL preprocessor // Flag to indicate whether this is a preprocessor or postprocessor
- )
- {
- int i, j;
- int N = getIntParameter(instance, 2, 1);
- DecimatorData *bufferData = getUserData(instance);
- NSFloat result;
-
- for (i=N-1; i>0; i--)
- for (j=0; j<bufferData->length; j++)
- buffer(i,j) = buffer(i-1,j);
- for (j=0; j<bufferData->length; j++)
- buffer(0,j) = input[j];
- if (++bufferData->count >= N) {
- bufferData->count = 0;
- if (!preprocessor)
- for (j=0; j<bufferData->length; j++)
- output[j] = 0.0f;
- for (j=0; j<bufferData->length; j++) {
- result = 0.0f;
- for (i=0; i<N; i++)
- result += buffer(i,j);
- output[j] = result/N;
- }
- return TRUE;
- }
- return FALSE;
- }
-
- /******************************************/
- /* Called every time the network is reset */
- __declspec(dllexport) void networkReset(
- DLLData *instance // Pointer to instance data (may be NULL)
- )
- {
- int i, j;
- int N = getIntParameter(instance, 2, 1);
- DecimatorData *bufferData = getUserData(instance);
-
- for (i=0; i<N; i++)
- for (j=0; j<bufferData->length; j++)
- buffer(i,j) = 0.0f;
- bufferData->count = 0;
- }
-
- /******************************************/
- /* Management of instance data (OPTIONAL) */
- __declspec(dllexport) DLLData *allocPrePost(
- DLLData *oldInstance, // Pointer to the last instance if reallocating
- int *rows, // Number of rows of output data, can be changed to reflect a diffenent number for the input data
- int *cols, // Number of cols of output data, can be changed to reflect a diffenent number for the input data
- BOOL preprocessor // Flag to indicate whether this is a preprocessor or postprocessor
- )
- {
- DLLData *instance = allocDLLInstance(oldInstance);
- DecimatorData *bufferData = calloc(1,sizeof(DecimatorData));
- setParameterName(instance, 2, 1, "N", TRUE);
- setIntParameter(instance, 2, 1, 4, FALSE);
- bufferData->length = *rows * *cols;
- bufferData->data = calloc(bufferData->length*getIntParameter(instance, 2, 1), sizeof(NSFloat));
- setUserData(instance, bufferData);
- return instance;
- }
-
- __declspec(dllexport) void freePrePost(DLLData *instance)
- {
- DecimatorData *bufferData = getUserData(instance);
- if (bufferData) {
- free(bufferData->data);
- free(bufferData);
- }
- freeDLLInstance(instance);
- }
-